package buaa.paas.kubectl;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import com.github.dockerjava.transport.DockerHttpClient;
import org.apache.commons.lang.SystemUtils;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
public class DockerUtil {
    private DockerClientConfig config;
    private DockerClient client;

    public DockerUtil() {
        final String localDockerHost = SystemUtils.IS_OS_WINDOWS ? "tcp://localhost:2375" : "unix:///var/run/docker.sock";
        config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost(localDockerHost).build();
        DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .sslConfig(config.getSSLConfig())
                .maxConnections(100)
                .connectionTimeout(Duration.ofSeconds(30))
                .responseTimeout(Duration.ofSeconds(45))
                .build();
        client = DockerClientImpl.getInstance(config, httpClient);
    }

    public DockerClient getClient() {
        return client;
    }
}
